# import ddddocr

# # 创建DdddOcr实例
# ocr = ddddocr.DdddOcr(show_ad=False)

# # 打开验证码图片文件，并读取为二进制数据
# with open("captcha.png", "rb") as f:
#     img_bytes = f.read()

# # 使用classification方法进行预测
# result = ocr.classification(img_bytes)
# print(f"识别结果: {result}")
# ------------------------------------------------------------------------------------------------------------------
# from datetime import datetime, timedelta

# # 获取当前时间 T，并清空到当天的 00:00:00
# T = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)

# # 定义两个关键时间节点
# start_date = T - timedelta(days=7)  # T - 7 天 的 00:00:00
# end_date = T - timedelta(days=38)  # T - 38 天 的 00:00:00

# # 生成从 start_date 到 end_date 的时间序列，每7天一个节点（包含首尾）
# current = start_date
# date_list = []

# while current >= end_date:
#     date_list.append(current)
#     current -= timedelta(days=7)

# # 输出结果
# for dt in date_list:
#     print(dt.strftime("%Y-%m-%d %H:%M:%S"))

# ------------------------------------------------------------------------------------------------------------------
# import cv2
# import ddddocr
# import numpy as np

# ocr = ddddocr.DdddOcr(show_ad=False)
# img_list = ["captcha.png", "captcha1.png", "captcha2.png"]

# for img in img_list:
#     # 读取图像
#     image = cv2.imread(img)

#     # 将图像从BGR转换为HSV
#     hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

#     # 定义红色的HSV范围
#     lower_red_1 = np.array([0, 100, 100])
#     upper_red_1 = np.array([10, 255, 255])
#     lower_red_2 = np.array([160, 100, 100])
#     upper_red_2 = np.array([180, 255, 255])

#     # 创建掩膜
#     mask1 = cv2.inRange(hsv, lower_red_1, upper_red_1)
#     mask2 = cv2.inRange(hsv, lower_red_2, upper_red_2)

#     # 合并两个掩膜
#     mask = mask1 + mask2

#     kernel = np.ones((3, 3), np.uint8)
#     # 对掩膜进行膨胀操作，以连接邻近的红色区域
#     mask = cv2.dilate(mask, kernel, iterations=1)
#     # 应用掩膜到原始图像
#     red_only = cv2.bitwise_and(image, image, mask=mask)
#     # 创建白色背景图像，大小与原图相同
#     white_background = np.ones_like(image) * 255

#     # 使用掩膜将红色部分叠加到白色背景上
#     # 首先反转掩膜，使得非红色区域变为白色
#     inverse_mask = cv2.bitwise_not(mask)
#     # 将白色背景应用到非红色区域
#     white_background_with_holes = cv2.bitwise_and(
#         white_background, white_background, mask=inverse_mask
#     )
#     # 将红色部分和白色背景合并
#     result = cv2.add(red_only, white_background_with_holes)

#     # 显示结果或保存结果
#     # cv2.imshow("Red on White Background", result)
#     cv2.imwrite(f"{img}.jpg", result)

#     with open(f"{img}.jpg", "rb") as f:
#         img_bytes = f.read()

#     res = ocr.classification(img_bytes)
#     print(res)
#     # 创建DdddOcr实例
#     ocr = ddddocr.DdddOcr(show_ad=False)
#     # 使用classification方法进行预测
#     result = ocr.classification(img_bytes)
#     print(f"图片{img}验证码识别结果: {result}")
# ------------------------------------------------------------------------------------------------------------------

import subprocess
import time
from playwright.sync_api import sync_playwright

# Step 1: 启动 Chrome（或 Chromium）
browser_port = 9222
# browser_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
browser_path = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"

process = subprocess.Popen(
    [
        browser_path,
        f" --remote-debugging-port={browser_port}",
        "https://www.baidu.com",
    ]
)

# 等待浏览器完成初始化
time.sleep(3)

# Step 2: 使用 Playwright 连接该浏览器
with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp(f"http://localhost:{browser_port}")
    
    default_context = browser.contexts[0]
    page = default_context.pages[0]

    # 执行一些操作
    print("当前页面 URL:", page.url)
    print("当前页面标题:", page.title())

    # 截图
    page.screenshot(path="connected_browser_screenshot.png")

# 如果你想关闭浏览器，可以取消注释以下代码
# process.terminate()
